package com.hh.controller;

import com.hh.dto.ResultDTO;
import com.hh.service.CoachService;
import com.hh.service.NewsService;
import com.hh.until.Tool;
import com.hh.vo.CoachVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/back/coach")
public class CoachController {
    @Autowired
    private CoachService coachService;

    @GetMapping("/findAll")
    public ResultDTO findAll(CoachVO coachVO) {
        coachVO.setEnable(Tool.nullToInt(coachVO.getEnable()));
        coachVO.setPage(Tool.strToPage(coachVO.getPage().toString()));
        coachVO.setLimit(Tool.strToLimit(coachVO.getLimit().toString()));

        ResultDTO resultDTO = coachService.findAll(coachVO);
        return resultDTO;
    }

    @GetMapping("/findById")
    public ResultDTO findById(CoachVO coachVO) {
        coachVO.setId(Tool.strToInt(coachVO.getId().toString()));

        ResultDTO resultDTO = coachService.findById(coachVO);
        return resultDTO;
    }

    @PostMapping("/add")
    public ResultDTO add(CoachVO coachVO) {
        coachVO.setName(Tool.nullToStr(coachVO.getName()));
        coachVO.setAge(Tool.strToInt(coachVO.getAge().toString()));
        coachVO.setSex(Tool.nullToStr(coachVO.getSex()));
        coachVO.setGradeId(Tool.nullToInt(coachVO.getGradeId()));
        coachVO.setPhone(Tool.nullToStr(coachVO.getPhone()));
        coachVO.setEmail(Tool.nullToStr(coachVO.getEmail()));
        coachVO.setEnable(Tool.nullToInt(coachVO.getEnable()));
        coachVO.setImg(Tool.nullToStr(coachVO.getImg()));

        ResultDTO resultDTO = coachService.add(coachVO);
        return resultDTO;
    }

    @PostMapping("/delete")
    public ResultDTO delete(CoachVO coachVO) {
        coachVO.setId(Tool.strToInt(coachVO.getId().toString()));

        ResultDTO resultDTO = coachService.delete(coachVO);
        return resultDTO;
    }

    @PostMapping("/update")
    public ResultDTO update(CoachVO coachVO) {
        coachVO.setId(Tool.strToInt(coachVO.getId().toString()));
        coachVO.setName(Tool.nullToStr(coachVO.getName()));
        coachVO.setAge(Tool.strToInt(coachVO.getAge().toString()));
        coachVO.setSex(Tool.nullToStr(coachVO.getSex()));
        coachVO.setGradeId(Tool.nullToInt(coachVO.getGradeId()));
        coachVO.setPhone(Tool.nullToStr(coachVO.getPhone()));
        coachVO.setEmail(Tool.nullToStr(coachVO.getEmail()));
        coachVO.setEnable(Tool.nullToInt(coachVO.getEnable()));
        coachVO.setImg(Tool.nullToStr(coachVO.getImg()));

        ResultDTO resultDTO = coachService.update(coachVO);
        return resultDTO;
    }

    @GetMapping("/count")
    public ResultDTO count(CoachVO coachVO) {
        coachVO.setName(Tool.nullToStr(coachVO.getName()));
        coachVO.setEnable(Tool.nullToInt(coachVO.getEnable()));

        ResultDTO resultDTO = coachService.count(coachVO);
        return resultDTO;
    }
}
